﻿package app.view.vc 
{
    import app.view.event.UIEvent;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.Bitmap;
    //import br.com.stimuli.loading.*;
    import app.model.vo.Config;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
	
	import org.lzyy.util.SequenceLoader;
	import org.lzyy.util.SequenceLoaderEvent;
    /**
    * Author lzyy
    * Email:healdream@gmail.com
    * Blog:http://lab.lzyy.name/blog
    */
    public class Img extends Sprite
    {
        public static const IMG_CLICKED:String = 'img_clicked';
        public static const IMG_LOADED:String = 'img_loaded';
        public static const NEXT_IMG:String = 'next_img';
        private var _loader:SequenceLoader = new SequenceLoader();
        private var _imgsArr:Array = new Array();
		private var _imgCntArr:Array = new Array();//存放图片的数组
        public var currentImg:Number = 0;
        public var prevImg:Number = 0;
        private var _preLoader = new preLoader();
        private var _stageWidth:Number;
        private var _stageHeight:Number;
        private var _timer:Timer = new Timer(5000);
        public function Img() 
        {
            
        }
        
        public function startAutoSwitch()
        {
            _timer.addEventListener(TimerEvent.TIMER, _onTimerNext);
            _timer.start();
        }
		
		public function pauseAutoSwitch()
		{
			_timer.stop();
			_timer.start();
		}
        
        private function _onTimerNext(evt:TimerEvent)
        {
            ++currentImg;
            if (currentImg >= _imgsArr.length)
            {
                currentImg = 0;
            }
            dispatchEvent(new Event(NEXT_IMG));
        }
        
        public function getStageInfo(w:Number,h:Number)
        {
            _stageHeight = h;
            _stageWidth = w;
        }
        
        public function loadImgs(imgs:Array)
        {
            _showLoading();
            _imgsArr = imgs;
            _loader.addEventListener(SequenceLoaderEvent.UNITCOMPLETE, _addImgs);
            _loader.addEventListener(Event.COMPLETE, showImgs);
            _loader.start(_imgsArr);
        }
		
		private function _addImgs(evt:SequenceLoaderEvent)
		{
			_imgCntArr.push(evt.data.content);
		}
        
        private function _showLoading()
        {
            addChild(_preLoader);
            _preLoader.x = _stageWidth/2 - _preLoader.width/2;
            _preLoader.y = _stageHeight/2 - _preLoader.height/2;
        }
        
        private function _hideLoading()
        {
            removeChild(_preLoader);
        }
        
        public function showImgs(evt)
        {
            _hideLoading();
            for (var i = 0; i < _imgsArr.length; i++)
            {
                var imgs:MovieClip = new MovieClip();
                imgs.buttonMode = true;
                imgs.i = i;
                imgs.name = 'img' + i;
                imgs.addEventListener(MouseEvent.CLICK, _onImgClick);
                var bmp:Bitmap = Bitmap(_imgCntArr[i]);
                bmp.width = _getImgWidth();
                bmp.height = _getImgHeight();
                imgs.addChild(bmp);
                imgs.alpha = 0;
                addChild(imgs);
            }
            
            if (Config.SWITCH_TYPE == 'fade')
            {
                
            }
            dispatchEvent(new Event(IMG_LOADED));
        }
        
        public function _onImgClick(evt:MouseEvent)
        {
            var dest = MovieClip(evt.currentTarget);
            dispatchEvent(new UIEvent(IMG_CLICKED,{index:dest.i}));
        }
        
        public function switchImg()
        {
            
        }
        
        private function _getImgWidth()
        {
            var w:Number;
            if (Config.IMG_WIDTH != 0)
            {
                w = Config.IMG_WIDTH;
            }
            else
            {
                w = _stageWidth;
            }
            return w;
        }
        
        private function _getImgHeight()
        {
            var h:Number;
            if (Config.IMG_HEIGHT != 0)
            {
                h = Config.IMG_HEIGHT;
            }
            else
            {
                h = _stageHeight;
            }
            return h;
        } 
        
    }
    
}